package com.example.resource.api;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 请求返回封装
 *
 * @param <T>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiResult<T> {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 返回信息
     */
    private String msg;
    /**
     * 返回的数据
     */
    private T data;

    public static <T> ApiResult<T> ok() {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(200);
        apiResult.setMsg("请求成功");
        return apiResult;
    }

    public static <T> ApiResult<T> ok(T data, String... msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(200);
        apiResult.setData(data);
        apiResult.setMsg("请求成功");
        if (msg != null && msg.length > 0) {
            apiResult.setMsg(msg[0]);
        }
        return apiResult;
    }


    public static <T> ApiResult<T> badRequest() {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(400);
        apiResult.setMsg("请求错误");
        return apiResult;
    }

    public static <T> ApiResult<T> badRequest(T data, String... msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(400);
        apiResult.setData(data);
        apiResult.setMsg("请求错误");
        if (msg != null && msg.length > 0) {
            apiResult.setMsg(msg[0]);
        }
        return apiResult;

    }
}